using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Entity;

using System.Data;
namespace DAL
{
    public class SysFunDAL
    {
        /// <summary>
        /// 配置单件实例
        /// </summary>
        public static readonly SysFunDAL Instance = new SysFunDAL();

        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="isDir"></param>
        /// <returns></returns>
        public DataTable GetFunForIsDir(string isDir)
        {
            DataTable dt = null;
            try
            {
                dt = SqlHelper.ExecuteDataSet("select id,name,parent_id from sys_fun where is_Dir='" + isDir + "' order by sort asc ").Tables[0];
            }
            catch (Exception ee)
            {
            }
            return dt;
        }
        /// <summary>
        /// 查询所有配置
        /// </summary>
        /// <returns></returns>
        public List<SysFun> SelectSysFunction(string LoginAccount)
        {
            SysFun sysFunctionEntity = null;
            List<SysFun> listConfig = new List<SysFun>();
            SqlDataReader sdr = null;

            try
            {

                SqlParameter[] parms = new SqlParameter[]{
                    new SqlParameter("@LoingName", SqlDbType.NVarChar),
                };
                parms[0].Value = LoginAccount;
                string SQL_SELECT_FUNCTION = "select f1.id as id,parent_id,name,url,sort,is_dir,is_view,remark,f1.logo  from Sys_Fun f1";
                SQL_SELECT_FUNCTION += " left join Sys_User_Fun uf on uf.fun_id=f1.id where  f1.is_view='1' AND uf.user_login_name=@LoingName order by sort asc";
                
                //执行查询语句
                using (sdr =

                    SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction,
                    System.Data.CommandType.Text, SQL_SELECT_FUNCTION, parms))
                {
                    while (sdr.Read())
                    {
                        sysFunctionEntity = new SysFun()
                        {
                            ID = sdr.GetInt32(0),
                            Parent_id = sdr.GetInt32(1),
                            Name = sdr.GetString(2),
                            Url = sdr.GetString(3),
                            Is_dir = sdr.GetString(5),
                            Logo = sdr.IsDBNull(8)?"form.png": sdr.GetString(8)
                        };
                        listConfig.Add(sysFunctionEntity);
                    }
                }
            }
            catch
            {

            }
            finally
            {
                if (sdr != null && !sdr.IsClosed)
                    sdr.Close();
            }

            return listConfig;
        }
       
    }
}
